/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.ce.managedbeans;

import com.bionic.ce.webservice.CurrencyRate;
import com.bionic.ce.webservice.WebService;
import java.io.IOException;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Schedule;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class WebServiceManagedBean implements Serializable {
//инициализация списка курсов обмена при инициализация WebServiceManagedBean

    public WebServiceManagedBean() {
        upadateRates();
    }
    private static List<CurrencyRate> rateList = new ArrayList<CurrencyRate>();
    private String txDate = new String();

    public List<CurrencyRate> getRateList() {
        return rateList;
    }

    public void setRateList(List<CurrencyRate> rateList) {
        this.rateList = rateList;
    }

    public String getTxDate() {
        return txDate;
    }

    public void setTxDate(String txDate) {
        this.txDate = txDate;
    }
// получение списка курсов обмена из веб-сервиса

    public List<CurrencyRate> downloadRateList() {
        rateList = new ArrayList<CurrencyRate>();
        try {
            rateList = new WebService().createRateList();
        } catch (MalformedURLException ex) {
            //NOP
        } catch (IOException ex) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Web service is currently unavailable", ""));
        }
        return rateList;
    }
// обновление ежедневно в 10.00 списка курсов
    @Schedule(hour = "10", minute = "00")
    public void upadateRates() {
        Date date = new Date();
        SimpleDateFormat dtFrm = new SimpleDateFormat("dd.MM.yyyy HH:mm");
        txDate = dtFrm.format(date);
        rateList = downloadRateList();
    }
}
